package com.douwong.student.base;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.interfaces.QuestionAnswerInteractInterface;
import com.douwong.student.model.QaTopic;
import com.douwong.student.popuwindow.ImageViewPopuwidow;
import com.douwong.student.popuwindow.ShowQADetailPopuWindow;
import com.douwong.student.utils.ImagFilterUtil;
import com.douwong.student.utils.UIUtils;
import com.douwong.student.view.MTextView;

import java.io.File;

/**
 * @autor yichao
 * @time 2017/4/515:34
 * @des 问答互动选择题基类
 */
public abstract class BaseQuestionAnswerChoiceFragment extends BaseFragment implements QuestionAnswerInteractInterface{
    private static final String TAG = "BaseChoiceFragment";

    protected int[] linearlayouts = new int[] { R.id.ll_A_choice, R.id.ll_B_choice, R.id.ll_C_choice, R.id.ll_D_choice,
            R.id.ll_E_choice, R.id.ll_F_choice, R.id.ll_G_choice, R.id.ll_H_choice };
    protected int[] textViews = new int[] { R.id.tv_choose_A, R.id.tv_choose_B, R.id.tv_choose_c, R.id.tv_choose_D,
            R.id.tv_choose_e, R.id.tv_choose_F, R.id.tv_choose_g, R.id.tv_choose_H};
    protected String[] chart = { "A", "B", "C", "D", "E", "F", "G", "H" };
    protected ImageView mIvHead;
    private MTextView mMtTopic;
    private LinearLayout mLlTitleRoot;
    protected View mContenView;
    private Context mContext;
    private QaTopic mQaTopic;
    private File mFile;
    private ShowQADetailPopuWindow mShowQADetailPopuWindow;

    public BaseQuestionAnswerChoiceFragment(Context context, QaTopic qaTopic) {
        super();
        mContext = context;
        mQaTopic = qaTopic;
    }

    @Override
    protected View getRootView(LayoutInflater inflater, ViewGroup container) {
        mContenView = UIUtils.inflate(getActivity(), R.layout.choice_questions);
        mIvHead = (ImageView) mContenView.findViewById(R.id.iv_answer_show_pic);
        mMtTopic = (MTextView) mContenView.findViewById(R.id.tv_question_choice);
        mLlTitleRoot = (LinearLayout) mContenView.findViewById(R.id.ll_choicefragme_title_root);
        return mContenView;
    }

    @Override
    protected void initData() {
        super.initData();
        boolean showIcon = isShowIcon( mQaTopic);
        if (showIcon){
            mIvHead.setVisibility(View.VISIBLE);
        }
        //题目数据
        ImagFilterUtil.filterMTImage(mMtTopic,mQaTopic.getBody());
        //选项
        for (int i = 0; i < mQaTopic.getOptions().size(); i++) {
            LinearLayout linearLayoutView = (LinearLayout) mContenView.findViewById(linearlayouts[i]);
            linearLayoutView.setVisibility(View.VISIBLE);
            String option = mQaTopic.getOptions().get(i);
            String[] split = option.split(":");
            String s="";
            if (split!=null && split.length>=2){
                s=split[1];
            }
            ImagFilterUtil.filterImage((TextView) mContenView.findViewById(textViews[i]),s,1);
        }
    }

    /**
     * 是否显示图片
     * @param qaTopic
     * @return
     */
    private boolean isShowIcon(QaTopic qaTopic) {
        if (qaTopic!=null&&qaTopic.getImageUrl()!=null&&qaTopic.getImageUrl().length()!=0){
            return true;
        }
        return false;
    }

    @Override
    public void setImageView(File file, Bitmap bitmap) {
        mFile =file;
        mIvHead.setImageBitmap(bitmap);
    }
    @Override
    protected void initEvent() {
        super.initEvent();
        for (int i = 0; i < linearlayouts.length; i++) {
            mContenView.findViewById(linearlayouts[i]).setOnClickListener(mOnClickListener);
        }
        mIvHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mFile !=null){
                    ImageViewPopuwidow imageViewPopuwidow=new ImageViewPopuwidow(mContext, mFile);
                    imageViewPopuwidow.showAtLocation(getView(), Gravity.CENTER, 0, 0);
                }
            }
        });

        mLlTitleRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //进入详情

                mShowQADetailPopuWindow = new ShowQADetailPopuWindow(mContext,mQaTopic.getSubjectType(),mQaTopic.getBody(),mQaTopic.getOptions(),mFile==null?null:mFile.getAbsolutePath());
                mShowQADetailPopuWindow.showAtLocation(v,Gravity.CENTER,0,0);



            }
        });

    }
    View.OnClickListener mOnClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_A_choice:
                    setSlectViewState("A");
                    break;
                case R.id.ll_B_choice:
                    setSlectViewState("B");
                    break;
                case R.id.ll_C_choice:
                    setSlectViewState("C");
                    break;
                case R.id.ll_D_choice:
                    setSlectViewState("D");
                    break;
                case R.id.ll_E_choice:
                    setSlectViewState("E");
                    break;
                case R.id.ll_F_choice:
                    setSlectViewState("F");
                    break;
                case R.id.ll_G_choice:
                    setSlectViewState("G");
                    break;
                case R.id.ll_H_choice:
                    setSlectViewState("H");
                    break;
            }

        }
    };



    /**
     * 选项点击后的状态
     * @param str
     */
    protected abstract void setSlectViewState(String str);
}
